using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class TextNode : MonoBehaviour
{
    public Queue<TextNode> queue;
    public UnityEngine.UI.Image[] images;

    Quaternion rot180 = Quaternion.Euler(0, 180, 0);
    Vector3 offset;
    private void Awake()
    {
        offset = (Vector3.up - Vector3.forward);
        images = new UnityEngine.UI.Image[transform.childCount];
        for (int i = 0; i < images.Length; i++)
        {
            images[i] = transform.GetChild(i).GetComponent<UnityEngine.UI.Image>();
        }
    }
    public void DoBuble(float scale = 1f)
    {
        Vector3 s = new Vector3(1, 0.8f, 1) * scale;
        
        for (int i = 0; i < images.Length; i++)
        {
            //images[i].DOFade(1, 0);
        }

        transform.localScale = s;
        transform.DOScale(s * 3f, 0.1f).onComplete = () =>
        {
            transform.DOScale(s * 2.5f, 0.1f);
        };

        transform.DOMove(transform.position + offset + UnityEngine.Random.insideUnitSphere * 2, 1).onComplete = () =>//DOMoveY(transform.position.y + 1, 1)
        {
            

            images[0].DOFade(0, 1).onComplete = () =>
            {
                OnDis();
            };
            for (int i = 1; i < images.Length; i++)
            {
                images[i].DOFade(0, 1);
            }
        };

    }
    public void AlignCamera()
    {
        transform.rotation = Camera.main.transform.rotation;
    }
    public void OnDis()
    {
        //ObjectPoolManager.Instance.PushTextNode(this);
        //queue.Enqueue(this);
        //this.gameObject.SetActive(false);
    }



    public void SetText(string text, bool isRed)
    {
        for (int i = 0; i < images.Length; i++)
        {
            images[i].gameObject.SetActive(false);
        }
        if (isRed)
        {
            for (int i = 0; i < text.Length; i++)
            {
                images[i].gameObject.SetActive(true);
                images[i].sprite = GetRedSprite(text[i]);
            }
        }
        else
        {
            for (int i = 0; i < text.Length; i++)
            {
                images[i].gameObject.SetActive(true);
                images[i].sprite = GetSprite(text[i]);
            }
        }

    }

    Sprite GetSprite(char car)
    {
        switch (car)
        {
            //case '0': return BasicDataManger.Instance.whiteText[0];
            //case '1': return BasicDataManger.Instance.whiteText[1];
            //case '2': return BasicDataManger.Instance.whiteText[2];
            //case '3': return BasicDataManger.Instance.whiteText[3];
            //case '4': return BasicDataManger.Instance.whiteText[4];
            //case '5': return BasicDataManger.Instance.whiteText[5];
            //case '6': return BasicDataManger.Instance.whiteText[6];
            //case '7': return BasicDataManger.Instance.whiteText[7];
            //case '8': return BasicDataManger.Instance.whiteText[8];
            //case '9': return BasicDataManger.Instance.whiteText[9];
            //case '.': return BasicDataManger.Instance.whiteText[10];
            //case 'K': return BasicDataManger.Instance.whiteText[11];
            //case 'M': return BasicDataManger.Instance.whiteText[12];
        }
        return null;
    }

    Sprite GetRedSprite(char car)
    {
        switch (car)
        {
            //case '0': return BasicDataManger.Instance.redText[0];
            //case '1': return BasicDataManger.Instance.redText[1];
            //case '2': return BasicDataManger.Instance.redText[2];
            //case '3': return BasicDataManger.Instance.redText[3];
            //case '4': return BasicDataManger.Instance.redText[4];
            //case '5': return BasicDataManger.Instance.redText[5];
            //case '6': return BasicDataManger.Instance.redText[6];
            //case '7': return BasicDataManger.Instance.redText[7];
            //case '8': return BasicDataManger.Instance.redText[8];
            //case '9': return BasicDataManger.Instance.redText[9];
            //case '.': return BasicDataManger.Instance.redText[10];
            //case 'K': return BasicDataManger.Instance.redText[11];
            //case 'M': return BasicDataManger.Instance.redText[12];
            //case 'X': return BasicDataManger.Instance.redText[13];
        }
        return null;
    }

    internal void Init(Vector3 pos, string text, bool isRed)
    {
        SetText(text, isRed);
        transform.position = pos;
        transform.SetParent(UIManager.Instance.ThreeDCanvas.transform);
        transform.localScale = Vector3.one;
    }
}
